package com.usf.jprovider.schema.provider.adapter.date;

import java.util.Date;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlSeeAlso;

import org.apache.commons.lang3.time.DateUtils;

import com.usf.jprovider.object.JObjectNode;
import com.usf.jprovider.object.env.Environment;
import com.usf.jprovider.object.type.JDate;
import com.usf.jprovider.object.type.JType;
import com.usf.jprovider.schema.provider.adapter.AbstractProviderAdapter;
import com.usf.jprovider.schema.provider.random.date.ToDay;
import com.usf.jprovider.schema.type.DBDate;
import com.usf.jprovider.tools.DateTools;

@XmlSeeAlso({
	BeforeDateAdapter.class,
	AfterDateAdapter.class
})
public abstract class AbstractDateAdapter extends AbstractProviderAdapter {
	
	@XmlAttribute protected int count;

	public AbstractDateAdapter() {
		super();
		provider = new ToDay();
		type = new DBDate();
	}
	public int getCount() {
		return count;
	}
	
	@Override
	public final JDate create(Environment env, JObjectNode node) throws Exception {
		JType<?> value = provider.create(env, node);
		Date date = DateTools.valueOf(value.getFormatedValue(), value.getFormat());
		return new JDate(DateUtils.addDays(date, count()), type.getFormat());
	}
	@Override
	public final boolean checkValue(Environment env, JObjectNode node, JType<?> value) throws Exception {
		return value.equals(create(env, node));
	}
	
	protected abstract int count();
}
